package com.apestech.framework.sql.table;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.db.callback.TableJsonCallBackHandler;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.SqlUtil;
import com.apestech.framework.sql.lib.TableSql;
import com.apestech.framework.xml.table.modul.MasterTable;
import com.apestech.framework.xml.table.modul.Tables;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-01-18-15:56
 */
public class MasterTableQuery extends SqlUtil {
    private SqlSession session;
    private Tables table;
    private MasterTable.SubTable subtable;
    private String tabname;
    private JSONObject input;
    private JSONArray qlist;
    private TableJsonCallBackHandler tableJsonCallBackHandler;

    public MasterTableQuery(SqlSession session, TableSql ddlsql, JSONObject input) throws Exception {
        this.session=session;
        this.input=input;
        table=ddlsql.getTable();
        subtable=table.getMaster().getTable();
        tabname=table.getTabName(table.getMasterTable());
        if(tabname==null) tabname=((Tables.TableRelat)table.getRelat().get(table.getRelat().getFirstKey())).getName();
    }

    public MasterTableQuery(SqlSession session, Tables table, String tabname, MasterTable.SubTable subtable, JSONObject input) throws Exception {
        this.session=session;
        this.table=table;
        this.tabname=tabname;
        this.input=input;
        this.subtable=subtable;
    }

    public TableJsonCallBackHandler getTableJsonCallBackHandler() {
        return tableJsonCallBackHandler;
    }

    public void setTableJsonCallBackHandler(TableJsonCallBackHandler tableJsonCallBackHandler) {
        this.tableJsonCallBackHandler = tableJsonCallBackHandler;
    }

    public void query() throws Exception {
        if(input!=null && input.get(tabname)!=null){
            input.put("tablename", tabname);
            qlist=session.getForJSON().query(input.getString(tabname), input, tableJsonCallBackHandler);
            input.remove("tablename");
        } else {
            qlist=session.getForJSON().table(tabname, input, tableJsonCallBackHandler);
        }
        int len=qlist.size();
        if(len>0 && subtable.getSubTable()!=null){
            for(int i=0; i<len; i++){
                JSONObject param=qlist.get(i);
                ArrayList<String> klist=subtable.getSubTable().getOrderKeylist();
                int klen=klist.size();
                for(int k=0; k<klen; k++){
                    String tabid=klist.get(k);
                    String tname=table.getTabName(tabid);
                    if(tname==null) tname=((MasterTable.SubTable) subtable.getSubTable().get(tabid)).getName();
                    JSONObject p=getKeyValue(session, tabname, tname, param);
                    p.put(tname, input.getString(tname));
                    MasterTableQuery query=new MasterTableQuery(session, table, tname,
                                                                subtable.getSubTable(tabid),
                                                                p);
                    query.setTableJsonCallBackHandler(tableJsonCallBackHandler);
                    query.query();
                    if(query.getQlist()!=null) param.put(tname, query.getQlist());
                }
                qlist.set(i, param);
                if(tableJsonCallBackHandler!=null) tableJsonCallBackHandler.getSubTable(tabname, qlist.get(i));
            }
        }
    }

    public JSONArray getQlist() {
        return qlist;
    }
}
